#!/bin/env bash
source ../config
cd ${Path}
if CheckNetwork
then
  Git="gitee"
  GitMirror="mirrors.chenby.cn/https://github"
else
  Git="github"
  GitMirror="github"
fi
function InstallMiaoPlugin(){
  if [ ! -e plugins/miao-plugin/index.js ]
  then
    if ! git clone --depth=1 https://${Git}.com/yoimiya-kokomi/miao-plugin.git plugins/miao-plugin
    then
      echo -e ${red}安装失败${background}
      exit 1
    fi
  fi
}
function InstallTRSSGenshin(){
  if [ ! -e plugins/genshin/index.js ]
  then
    if ! git clone --depth=1 https://${git}.com/TimeRainStarSky/Yunzai-genshin.git plugins/genshin
    then
      echo -e ${red}安装失败${background}
      exit 1
    fi
  fi
}
function InstallNextGenshin(){
  if [ ! -e plugins/genshin/README.md ]
  then
    if ! git clone --depth=1 https://${GitMirror}.com/yunzai-org/genshin.git plugins/genshin
    then
      echo -e ${red}安装失败${background}
      exit 1
    fi
  fi
}
function InstallNextSystem(){
  if [ ! -e plugins/genshin/README.md ]
  then
    if ! git clone --depth=1 https://${GitMirror}.com/yunzai-org/system.git plugins/system
    then
      echo -e ${red}安装失败${background}
      exit 1
    fi
  fi
}
function InstallMiaoYunzai(){
  if [ ! -e Miao-Yunzai/package.json ]
  then
    if ! git clone --depth=1 https://${Git}.com/yoimiya-kokomi/Miao-Yunzai.git Miao-Yunzai
    then
      echo -e ${red}安装失败${background}
      exit 1
    fi
  fi
}
function InstallTRSSYunzai(){
  if [ ! -e TRSS-Yunzai/package.json ]
  then
    if ! git clone --depth=1 https://${Git}.com/TimeRainStarSky/Yunzai TRSS-Yunzai
    then
      echo -e ${red}安装失败${background}
      exit 1
    fi
  fi
}
function InstallYunzaiNext(){
  if [ ! -e Yunzai-Next/package.json ]
  then
    if ! git clone --depth=1 https://${GitMirror}.com/yunzai-org/yunzai-next.git Yunzai-Next
    then
      echo -e ${red}安装失败${background}
      exit 1
    fi
  fi
}
function PnpmPackage(){
  echo -e ${yellow}正在安装 ${green}依赖文件${background}
  if ! echo Y | pnpm install
  then
    echo -e ${red}错误${background}
    exit 1
  fi
  echo -en ${yellow}正在初始化${background}
  pnpm run start
  sleep 3s
  pnpm run stop
  rm -rf ~/.pm2/logs/*.log
  echo -en ${yellow}初始化完成${background}
  echo
}
function YarnPackage(){
  echo -e ${yellow}正在安装 ${green}依赖文件${background}
  if ! echo Y | yarn install
  then
    echo -e ${red}错误${background}
    exit 1
  fi
  echo -e ${yellow}正在编译${background}
  yarn build
  echo -en ${yellow}正在初始化${background}
  yarn start
  sleep 3s
  yarn stop
  rm -rf ~/.pm2/logs/*.log
  echo -en ${green}完成${background}
  echo
}
#此处仅为传参所用，如果开发者不满意此缩写，可联系ArcticFox520更改。
case $1 in
MZ)
  InstallMiaoYunzai
  cd Miao-Yunzai
  InstallMiaoPlugin
  PnpmPackage
;;
TZ)
  InstallTRSSYunzai
  cd TRSS-Yunzai
  InstallMiaoPlugin
  InstallTRSSGenshin
  PnpmPackage
;;
YN)
  InstallYunzaiNext
  cd Yunzai-Next
  InstallNextGenshin
  InstallMiaoPlugin
  YarnPackage
;;
esac